package com.example.day09d_1;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.DatabaseErrorHandler;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;

import androidx.annotation.Nullable;
import androidx.appcompat.widget.ToolbarWidgetWrapper;

import java.nio.DoubleBuffer;
import java.util.ArrayList;
import java.util.List;

public class InfoDataBase extends SQLiteOpenHelper {


    public InfoDataBase(Context context) {
        super(context,"allInfo.db",null,1);
    }

    @Override
    public void onCreate(SQLiteDatabase db) {
     db.execSQL("create table student (name varchar(20),sex varchar(20),age integer(3))");

    }

    @Override
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {

    }
    //插入
    public void initsert(Studio stu){
        SQLiteDatabase writableDatabase = getWritableDatabase();
        ContentValues values = new ContentValues();
        values.put("name",stu.getName());
        values.put("age",stu.getAge());
        values.put("sex",stu.getSex());
        writableDatabase.insert("student",null,values);
    }
    //删除
    public  void deleteStudent(Studio stu){
        SQLiteDatabase database = getWritableDatabase();
        database.delete("student","name=?",new String[]{stu.getName()});
    }
    //修改
    public  void updateStudent(String olName,Studio newStu){
        SQLiteDatabase writableDatabase = getWritableDatabase();
        ContentValues values = new ContentValues();
        values.put("name",newStu.getName());
        writableDatabase.update("student",values,"name=?",new String[]{olName});
    }
    //查询
    public List<Studio>selectStudio(){
        ArrayList<Studio> studios = new ArrayList<>();
        SQLiteDatabase sqLiteDatabase = getWritableDatabase();
        Cursor studio = sqLiteDatabase.query("student", null, null, null, null, null, null);
        while (studio.moveToNext()){
            String s = studio.getString(studio.getColumnIndex("name"));
            int age = studio.getInt(studio.getColumnIndex("age"));
            String sex = studio.getString(studio.getColumnIndex("sex"));
            studios.add(new Studio(s,sex,age));

        }

        return studios;
    }
}
